using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.Mvc3.Controllers
{
    [ValidateInput(false)]
    public class KitController:UI.BaseController
    {
         public const string ProgrameCode = "Kit";
         public static readonly string currentPath = UI.Utils.GetCurrentPath(ProgrameCode);

        public KitController()
        {
            ViewBag.CurrentPath = currentPath;
        }

         [UI.ValidateAuthority(ProgrameCode , "RunHQL")]
         public ViewResult RunHQL()
         {
             return View();
         }

         [HttpPost]
         [UI.ValidateAuthority(ProgrameCode , "RunHQL")]
         public ViewResult RunHQL(string Hql,string type)
         {
             ViewBag.Hql = Hql;
             var isession = BLL.DB_help.NHibernateHelper.GetCurrentSession("OA");
             try
             {
                 var iquery = isession.CreateQuery(Hql);
                 if (type == "command")
                 {
                     ViewBag.Message = iquery.ExecuteUpdate().ToString();
                 }
                 else if (type == "single")
                 {
                     ViewBag.Message = iquery.UniqueResult().ToString();
                 }
                 else
                 {
                     ViewBag.Data = iquery.List();
                     if (ViewBag.Data.Count == 0) ViewBag.Message = "未查询到任何数据";
                 }                 
             }
             catch (Exception e)
             {
                 ViewBag.Message = e.Message;
             }
             return View();
         }

         [UI.ValidateAuthority("SystemLog" , "view")]
         public ActionResult SystemLog(string path)
         {
             var basePath=Common.Keys.LogPath;
             if (basePath[basePath.Length - 1] != '/' && basePath[basePath.Length - 1] != '\\') basePath = basePath + "/";
             ViewBag.BasePath = basePath;
             ViewBag.Path = path;
             return View();
         }
    }
}
